package com.core.zyyz.amc.modules.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.core.common.utils.StringUtils;
import com.core.zyyz.amc.modules.domain.*;
import com.core.zyyz.amc.modules.service.AmcCodeManageService;
import com.core.zyyz.amc.modules.service.AmcSystemDimensionBService;
import com.core.zyyz.pcmc.domain.ValueSetValue;
import com.core.zyyz.pcmc.service.ValueSetValueService;
import com.core.zyyz.util.DictUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.core.common.annotation.Log;
import com.core.common.core.controller.BaseController;
import com.core.common.core.domain.AjaxResult;
import com.core.common.enums.BusinessType;
import com.core.zyyz.amc.modules.service.AmcSystemConfigBService;
import com.core.common.core.page.TableDataInfo;

/**
 * 功能页面配置主Controller
 *
 * @author LiuFushun
 * @date 2024-11-06
 */
@RestController
@RequestMapping("/amc/systemConfigB")
public class AmcSystemConfigBController extends BaseController
{
    @Autowired
    private AmcSystemConfigBService amcSystemConfigBService;
    @Autowired
    private AmcCodeManageService amcCodeManageService;
    @Autowired
    private AmcSystemDimensionBService amcSystemDimensionBService;
    @Autowired
    private ValueSetValueService valueSetValueService;

    /**
     * 查询功能页面配置主列表
     */
    @PreAuthorize("@ss.hasPermi('amc:systemConfigB:list')")
    @GetMapping("/list")
    public TableDataInfo list(AmcSystemConfigBVO param) {
        startPage();
        List<HashMap> list = amcSystemConfigBService.selectAmcSystemConfigBList(param);
        for (HashMap hashMap:list){
            if (!StringUtils.isNotEmpty(String.valueOf(hashMap.get("projectTypeName")))){
                String code = (String) hashMap.get("baseProjectType");
                String functionCode = (String) hashMap.get("baseBusinessNode");
                String dimensionId = "";
                if (StringUtils.isNotEmpty(functionCode)) {
                    AmcCodeManage amcCodeManage = amcCodeManageService.selectAmcCodeManageByFunctionCode(functionCode);
                    if (amcCodeManage != null) {
                        dimensionId = amcCodeManage.getDimensionId();
                    }
                }
                AmcSystemDimensionB dimensionB = amcSystemDimensionBService.selectAmcSystemDimensionBByDimensionId(dimensionId);
                if (dimensionB != null && ("1".equals(dimensionB.getDimensionType()) || "3".equals(dimensionB.getDimensionType()))) {
                    BaseDictionary dict = null;
                    if ("1".equals(dimensionB.getDimensionType())) {
                        String[] dictKey = dimensionB.getDimensionKey().split(",");
                        dict = DictUtils.get(dictKey[0], dictKey[1], code);
                        if (dict != null) {
                            hashMap.put("projectTypeName", dict.getLabel());
                        }
                    } else {
                        //从集值详情表中查询具体的值 value_set_value
                        ValueSetValue value = new ValueSetValue();
                        value.setValueSetCode(dimensionB.getDimensionKey());
                        value.setValueCode(code);
                        value = valueSetValueService.selectValueSetValueByKeys(value);
                        hashMap.put("projectTypeName", value.getValueName());
                    }
                }
            }
        }
        return getDataTable(list);
    }


    /**
     * 获取功能页面配置主详细信息
     */
    @PreAuthorize("@ss.hasPermi('amc:systemConfigB:query')")
    @GetMapping(value = "/{baseConfigId}")
    public AjaxResult getInfo(@PathVariable("baseConfigId") String baseConfigId)
    {
        return success(amcSystemConfigBService.selectAmcSystemConfigBByBaseConfigId(baseConfigId));
    }

    /**
     * 新增功能页面配置主
     */
    @PreAuthorize("@ss.hasPermi('amc:systemConfigB:add')")
    @Log(title = "功能页面配置主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AmcSystemConfigB amcSystemConfigB)
    {
        return toAjax(amcSystemConfigBService.insertAmcSystemConfigB(amcSystemConfigB));
    }

    /**
     * 修改功能页面配置主
     */
    @PreAuthorize("@ss.hasPermi('amc:systemConfigB:edit')")
    @Log(title = "功能页面配置主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AmcSystemConfigB amcSystemConfigB)
    {
        return toAjax(amcSystemConfigBService.updateAmcSystemConfigB(amcSystemConfigB));
    }

    /**
     * 删除功能页面配置主
     */
    @PreAuthorize("@ss.hasPermi('amc:systemConfigB:remove')")
    @Log(title = "功能页面配置主", businessType = BusinessType.DELETE)
    @DeleteMapping("/{baseConfigIds}")
    public AjaxResult remove(@PathVariable String[] baseConfigIds)
    {
        return toAjax(amcSystemConfigBService.deleteAmcSystemConfigBByBaseConfigIds(baseConfigIds));
    }
}
